package com.example.demo.controller;

import com.example.demo.entity.UserMes;
import com.example.demo.mapper.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author:王声基
 * @date:2019/12/25
 * @decription:用户控制层
 */
@Api(tags = "user")
@RestController
@EnableSwagger2
public class UserController {

    @Autowired
    private UserMapper userMapper;

    //添加用户且用户姓名不能重复
    @ApiOperation(value ="添加用户")
    @PutMapping("/AddUser")
    @Transactional
    public int AddUser(String username, String password, String email,String roles){
        if(userMapper.selectUserByName(username)!=null)
            return 1000;//姓名重复
        if(username!=null) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String currentDate = dateFormat.format( new Date() );
            Date gen_time =java.sql.Date.valueOf(currentDate);

            UserMes userMes=new UserMes();
            userMes.setUsername(username);
            userMes.setPassword(password);
            userMes.setEmail(email);
            userMes.setGenTime(gen_time);
            userMapper.insertUser(userMes);

            userMes.setuid(userMapper.selectUserByName(username).getuid());

            if(roles.indexOf("管理员")!=-1){
                userMapper.insertUserRole(userMes.getuid(),1);
            }
            if(roles.indexOf("用户")!=-1){
                userMapper.insertUserRole(userMes.getuid(),2);
            }
            return 0;
        }
        else {
            return 4004;
        }
    }

    //查询用户信息
    @ApiOperation(value ="通过id查看用户")
    @PutMapping("/SelectUserById")
    public UserMes SelectUserById(int uid){
        UserMes userMes=userMapper.selectUserById(uid);
        String string = "";
        for (int a:userMapper.selectUserRole(uid)) {
            string+=a;
        }
        userMes.setRole(string);
        return userMes;
    }

    //条件查询，通过用户名查询用户信息
    @ApiOperation(value ="通过用户名查询用户")
    @PutMapping("/SelectUserByUsername")
    public List<UserMes> SelectUserByUsername0(String username){
        return userMapper.selectUserByName0(username);
    }

    //删除指定id的用户
    @ApiOperation(value ="通过id删除用户")
    @PutMapping("/DeleteUserById")
    @Transactional
    public int DeleteUserById(int uid){
        return userMapper.deleteUserById(uid);
    }

    //修改用户信息
    @ApiOperation(value ="通过id更改用户信息")
    @PutMapping("/UpdateUserById")
    public int UpdateUserById(int uid,String username, String password, String email,String roles){
        userMapper.deleteUserRole(uid);
        if(roles.indexOf("管理员")!=-1){
            userMapper.insertUserRole(uid,1);
        }
        if(roles.indexOf("用户")!=-1){
            userMapper.insertUserRole(uid,2);
        }
        userMapper.updateUserById(uid,username,password, email);
        return 0;
    }
}
